package org.example.Swing.xiangmu.login;
import org.example.Entity.Score;
import org.example.Entity.Student;
import org.example.Swing.handler.UpdateHandler;
import org.example.controller.GradeController;
import javax.swing.*;
import java.awt.*;
import java.util.List;

public class UpdateView extends JDialog {
    JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 20));
    JLabel nameLabel = new JLabel("姓名:", JLabel.RIGHT);
    JTextField nameTxt = new JTextField();
    JLabel noLabel = new JLabel("学号:", JLabel.RIGHT);
    JTextField noTxt = new JTextField();
    JLabel homeLabel = new JLabel("课程:", JLabel.RIGHT);
    JTextField homeTxt = new JTextField();
    JLabel pscjLabel = new JLabel("平时成绩:", JLabel.RIGHT);
    JTextField psTxt = new JTextField();
    JLabel qmcjLabel = new JLabel("考试成绩:", JLabel.RIGHT);
    JTextField qmTxt = new JTextField();
    JButton xgBtn = new JButton("修改成绩");
    int courseIdTwo = 0;
    UpdateHandler updateHandler;

    public UpdateView(MainView mainView, String studentId, int courseId) {
        super(mainView, "修改成绩", true);
        updateHandler = new UpdateHandler(this);
        GradeController gradeController = new GradeController();
        courseIdTwo = courseId;
        List<Student> students = gradeController.selectTwoGrade(studentId, courseId);
        for (Student student : students) {
            nameTxt.setText(student.getName());
            noTxt.setText(student.getStudentId());
            List<Score> scores = student.getScores();
            for (Score score : scores) {
                homeTxt.setText(score.getCourse().getCourseName());
                psTxt.setText(String.valueOf(score.getScores()));
                qmTxt.setText(String.valueOf(score.getScores()));

            }
        }
        nameLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(nameLabel);
        nameTxt.setPreferredSize(new Dimension(200, 30));
        jPanel.add(nameTxt);

        noLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(noLabel);
        noTxt.setPreferredSize(new Dimension(200, 30));
        jPanel.add(noTxt);

        homeLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(homeLabel);
        homeTxt.setPreferredSize(new Dimension(200, 30));
        jPanel.add(homeTxt);

        pscjLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(pscjLabel);
        psTxt.setPreferredSize(new Dimension(200, 30));
        jPanel.add(psTxt);
        qmcjLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(qmcjLabel);
        qmTxt.setPreferredSize(new Dimension(200, 30));
        jPanel.add(qmTxt);

        xgBtn.addActionListener(updateHandler);
        jPanel.add(xgBtn);

        Container contentPane = getContentPane();
        contentPane.add(jPanel);
        homeTxt.setEnabled(false);
        noTxt.setEnabled(false);
        nameTxt.setEnabled(false);
        getRootPane().setDefaultButton(xgBtn);
        setSize(350, 500);
        setLocationRelativeTo(null);
        // DISPOSE_ON_CLOSE:只销毁当前的窗体
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public JTextField getStuId() {
        return noTxt;
    }

    public JTextField getPsTxt() {
        return psTxt;
    }

    public JTextField getKsTxt() {
        return qmTxt;
    }

    public int getCourseId() {
        return courseIdTwo;
    }
}
